<?php
/**
 * @name HackInPain_Engine_Router
 * @author François Guégnaud
 * 
 */
class HackInPain_Engine_Router extends HackInPain_Singleton_Abstract {
	
	/**
	 * Contants rules for routing.
	 * @var array('pre' => array(), 'post' => array())
	 */
	protected $_constant_routes;
	
	/**
	 * Rules for routing
	 * @var array('controller' => array())
	 */
	protected $_route_rules;
	
	
	/**
	 * Return all controllers who must be run
	 * @return array
	 */
	public function getRoutes() {
		// Routes constantes
		if(isset($this->_constant_routes['pre'])) {
			$pre_routes = $this->_constant_routes['pre'];
		} else $pre_routes = null;

		if(isset($this->_constant_routes['post'])) {
			$post_routes = $this->_constant_routes['post'];
		} else $post_routes = null;
		
		// Route en fonction du controlleur
		$gate = HackInPain_Engine_Gate::getInstance();
		$controller = $gate->getController();
		$action = $gate->getAction();
		$rules_routes = array(new HackInPain_Model_Route(array(
				'controller'	=> $controller,
				'action'		=> $action)));
		
		// TODO trancher entre ajax, partial et autre
		if(HackInPain_Engine_Gate::getFormat() == 'ajax' || HackInPain_Engine_Gate::getFormat() == 'partial') {
			$routes = $rules_routes;
		} else {
			if(!is_null($pre_routes)) $routes = array_merge($pre_routes, $rules_routes);
			if(!is_null($post_routes)) $routes = array_merge($routes, $post_routes);
		}

		return $routes;
	}
	
	/**
	 * ConstantRules setter
	 * @param array $rules
	 */
	public function __construct() {
		$engine = Application_Engine::getInstance();
		$this->_constant_routes = $engine->getConstantRoutes();
	}
}